package me.zhengjie.minbearwcs.dao.agv.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import lombok.RequiredArgsConstructor;
import me.zhengjie.minbearwcs.entity.DTO.agv.task.actionEnum.AgvAction;
import org.springframework.stereotype.Repository;
import me.zhengjie.minbearwcs.dao.agv.inf.AgvMissionDetailDao;
import me.zhengjie.minbearwcs.entity.Mapper.agv.AgvMissionDetailMapper;
import me.zhengjie.minbearwcs.entity.PO.agv.AgvMissionDetail;

import java.time.LocalDateTime;
import java.util.List;

@Repository
@RequiredArgsConstructor
public class AgvMissionDetailDaoImpl implements AgvMissionDetailDao {

    private final AgvMissionDetailMapper mapper;

    @Override
    public boolean insert(AgvMissionDetail agvMissionDetail) {
        return mapper.insert(agvMissionDetail) == 1;
    }

    @Override
    public AgvMissionDetail selectByTaskIdAndPoint(String taskId, String point) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(AgvMissionDetail::getTaskId,taskId)
                .eq(AgvMissionDetail::getPoint,point)
                .one();
    }

    @Override
    public boolean updateMarkTimeAndIsCompleteById(Integer id, LocalDateTime markTime, Integer isComplete) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(AgvMissionDetail::getId,id)
                .set(AgvMissionDetail::getMarkTime,markTime)
                .set(AgvMissionDetail::getIsComplete,isComplete)
                .update();
    }

    @Override
    public List<AgvMissionDetail> selectByTaskId(String taskId) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(AgvMissionDetail::getTaskId,taskId)
                .orderByAsc(AgvMissionDetail::getId)
                .list();
    }

    @Override
    public boolean deleteById(int id) {
        return mapper.deleteById(id) == 1;
    }

    @Override
    public AgvMissionDetail selectByTaskIdAndAction(String taskId, AgvAction action) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(AgvMissionDetail::getTaskId,taskId)
                .eq(AgvMissionDetail::getAction,action)
                .one();
    }

    @Override
    public AgvMissionDetail selectByPointAndAction(String point, AgvAction action) {
        return new LambdaQueryChainWrapper<>(mapper)
                .eq(AgvMissionDetail::getPoint,point)
                .eq(AgvMissionDetail::getAction,action)
                .one();
    }

    @Override
    public boolean updateMarkTimePointAndIsCompleteById(Integer id, String point, LocalDateTime markTime, Integer isComplete) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(AgvMissionDetail::getId,id)
                .set(AgvMissionDetail::getPoint,point)
                .set(AgvMissionDetail::getMarkTime,markTime)
                .set(AgvMissionDetail::getIsComplete,isComplete)
                .update();
    }

    @Override
    public boolean updatePointById(Integer id, String point) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(AgvMissionDetail::getId,id)
                .set(AgvMissionDetail::getPoint,point)
                .update();
    }

    @Override
    public boolean updatePointByTaskIdAndAction(String taskId, AgvAction action, String point) {
        return new LambdaUpdateChainWrapper<>(mapper)
                .eq(AgvMissionDetail::getTaskId,taskId)
                .eq(AgvMissionDetail::getAction,action)
                .set(AgvMissionDetail::getPoint,point)
                .update();
    }

}
